#include "stdafx.h"
#include "LayoutUtils.h"

#include "struct.h"

void nsLayoutUtils::rotateArea( SPEC_AREA * pStart, SPEC_AREA * pEnd, bool bToPortrait )
{
	CRect rect;
	CPoint pntOffset;

	int iWidth = min( TheWidth, TheHeight );

	while ( pStart != pEnd )
	{

		if ( ( pStart->side == 1 || pStart->side == 2 ) &&
			( pStart->left & pStart->top & pStart->right & pStart->bottom ) != ULONG_MAX )
		{
			if ( bToPortrait )		// landscape to portrait -> rotate clockwise
			{
				rect.left = iWidth - pStart->bottom - 1;
				rect.top = pStart->left;
				rect.right = iWidth - pStart->top - 1;
				rect.bottom = pStart->right;
				pntOffset.x = -pStart->nOffsetY;	// offset area (swap coordinates)
				pntOffset.y = pStart->nOffsetX;
			} else {				// portrait to landscape -> rotate counter-clockwise
				rect.left = pStart->top;
				rect.top = iWidth - pStart->right - 1;
				rect.right = pStart->bottom;
				rect.bottom = iWidth - pStart->left - 1;
				pntOffset.x = pStart->nOffsetY;
				pntOffset.y = -pStart->nOffsetX;
			}

			// write changes values back to structure
			pStart->left = rect.left;
			pStart->top = rect.top;
			pStart->right = rect.right;
			pStart->bottom = rect.bottom;

			pStart->nOffsetX = static_cast<short>( pntOffset.x );
			pStart->nOffsetY = static_cast<short>( pntOffset.y );
		}

		++pStart;
	}
}

